package cn.cvs.service.sysUser;

import cn.cvs.dao.sysUser.SysUserMapper;
import cn.cvs.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 示例8：接口实现类
 * 示例13：使用注解注入
 * 示例21：使用@Transactional注解处理事务
 */
@Transactional
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{
    @Autowired//@Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try{
            return sysUserMapper.selectSysUserList(sysUser);
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result = false;
        try{
            if (sysUserMapper.add(sysUser)==1){
                result =true;
                //测试事务回滚时，打开注释
//                throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public boolean del(Integer id) {
        boolean result = false;
        try {
            if (sysUserMapper.del(id)==1){
                result=true;
            }

        }catch (RuntimeException e){
            e.printStackTrace();
            throw  e;
        }
        return result;
    }

    @Override
    public boolean update(String password, Integer id) {
        boolean result = false;
        try {
            if (sysUserMapper.update(password,id)==1){
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw  e;
        }
        return result;
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        boolean result = false;
        try{
            if (sysUserMapper.updateUser(sysUser)==1){
                result=true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

}
